package org.example.dev.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.example.dev.mapper.UserMapper;
import org.example.dev.pojo.User;
import org.example.dev.service.EmailService;
import org.example.dev.service.IUserService;
import org.example.dev.service.MessageService;
import org.example.dev.service.VerificationCodeGenerator;
import org.springframework.stereotype.Service;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.Base64;

/**
 * @author 梁祖杰
 *
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements IUserService {

    private final UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     * @param phone
     * @param email
     */
    @Override
    public void register(String username, String password, String phone, String email, String avatar) {
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(password);
        newUser.setPhone(phone);
        newUser.setEmail(email);
        newUser.setAvatar(avatar);
        newUser.setCreatedAt(LocalDateTime.now());
        userMapper.insertUser(newUser);
    }

    /**
     * 登录
     * @param password
     * @param email
     * @return
     */
    @Override
    public User login(String password,String email) {
        User user = userMapper.findUserByEmail(email);
        // 如果user存在且密码正确
        if (user != null && password.equals(user.getPassword())&&email.equals(user.getEmail())) {
            return user;
        }
        return null;
    }
    @Override
    public void sendPhoneVerificationCode(String phone) {
        String verificationCode= null;
        try {
            verificationCode = VerificationCodeGenerator.generateTotp();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        MessageService.sendMessage("","",phone, verificationCode);
        System.out.println(verificationCode);
        userMapper.updatePhoneVerificationCode(phone,verificationCode,LocalDateTime.now());
    }

    @Override
    public String sendEmailVerificationCode(String email) {
        String verificationCode = null;
        try {
            verificationCode = VerificationCodeGenerator.generateTotp();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        EmailService.sendEmail(email,"验证码","文生图系统验证码为:"+verificationCode);
        return verificationCode;
    }


    @Override//改密码，若找不到邮箱或验证码错误都会返回null导致返回401
    public User resetPassword(String email, String password, String newPassword) {
        User user = userMapper.findUserByEmail(email);
        if(user.getPassword().equals(password)){
            userMapper.updatePassword(newPassword,LocalDateTime.now(),email);
            return user;
        }
        else {
            return null;
        }
    }

    @Override//忘记密码
    public User forgetPassword(String email, String password) {
        userMapper.updatePassword(password,LocalDateTime.now(),email);
        User user = userMapper.findUserByEmail(email);
        return user;
    }

    @Override
    public User getCurrentUser() {
        String id = StpUtil.getLoginId(StpUtil.getTokenInfo().getTokenValue());
        return userMapper.findUserById(Integer.parseInt(id));
    }

    @Override
    public User findUserByEmail(String email) {
        return userMapper.findUserByEmail(email);
    }

    @Override
    public User findUserByPhone(String phone) {
        return userMapper.findUserByPhone(phone);
    }

    @Override
    public void updateAvatar(User user){
        userMapper.updateAvatar(user.getAvatar(), user.getUserId());
    }

    @Override
    public User update(User user) {
        userMapper.update(user.getUsername(),user.getPhone(),user.getPassword(), user.getUserId());
        return user;
    }
}
